package com.summary.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.page.PageDomain;
import com.summary.utils.PaginationUtils;
import org.springframework.data.redis.connection.ReactiveZSetCommands;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.summary.domain.WeekSummary;
import com.summary.service.IWeekSummaryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 周报Controller
 *
 * @author ruoyi
 * @date 2024-01-15
 */
@RestController
@RequestMapping("/weekSummary")
public class WeekSummaryController extends BaseController
{
    @Autowired
    private IWeekSummaryService weekSummaryService;

    /**
     * 查询周报列表
     */
    @PreAuthorize("@ss.hasPermi('system:summary:list')")
    @GetMapping("/list")
    public TableDataInfo list(WeekSummary weekSummary)
    {
        startPage();
        List<WeekSummary> list = weekSummaryService.selectWeekSummaryList(weekSummary);
        return getDataTable(list);
    }


    /**
     * 导出周报列表
     */
    @PreAuthorize("@ss.hasPermi('system:summary:export')")
    @Log(title = "周报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WeekSummary weekSummary)
    {
        List<WeekSummary> list = weekSummaryService.selectWeekSummaryList(weekSummary);
        ExcelUtil<WeekSummary> util = new ExcelUtil<WeekSummary>(WeekSummary.class);
        util.exportExcel(response, list, "周报数据");
    }

    /**
     * 根据timeId获取周报详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:summary:query')")
    @GetMapping(value = "/{timeId}")
    public TableDataInfo getInfo(@PathVariable("timeId") Long timeId)
    {
        startPage();
        List<WeekSummary> list = weekSummaryService.selectWeekSummaryById(timeId);
        return getDataTable(list);
    }

    /**
     * 新增周报
     */
//    @PreAuthorize("@ss.hasPermi('system:summary:add')")

//    @Log(title = "周报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WeekSummary weekSummary)
    {
        return toAjax(weekSummaryService.insertWeekSummary(weekSummary));
    }

    /**
     * 修改周报
     */
    @PreAuthorize("@ss.hasPermi('system:summary:edit')")
    @Log(title = "周报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WeekSummary weekSummary)
    {
        return toAjax(weekSummaryService.updateWeekSummary(weekSummary));
    }

    /**
     * 删除周报
     */
    @PreAuthorize("@ss.hasPermi('system:summary:remove')")
    @Log(title = "周报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(weekSummaryService.deleteWeekSummaryByIds(ids));
    }


    /**
     * 根据userId查询用户是否在时间段内填写周报
     * @param userId
     * @return
     */
    @GetMapping("/getInfo/{userId}")
    public TableDataInfo getInfoByUserId(@PathVariable Long userId,PageDomain pageDomain)
    {
        List<WeekSummary> infoByUserId = weekSummaryService.getInfoByUserId(userId,pageDomain);
        List<WeekSummary> weekSummaryList = PaginationUtils.getPage(infoByUserId, pageDomain.getPageSize(), pageDomain.getPageNum());
        TableDataInfo dataInfo = new TableDataInfo();
        dataInfo.setTotal(infoByUserId.size());
        dataInfo.setCode(HttpStatus.SUCCESS);
        dataInfo.setMsg("查询成功");
        dataInfo.setRows(weekSummaryList);
        return dataInfo;
        //return getDataTable(infoByUserId);
    }


}
